package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.elasticsearch.search.SearchService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {
    //取到token后,将其作为key从redis中取出userinfo
    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;
    @Reference
    private IStrategyTemplateService strategyTemplateService;
    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;
    @Reference
    private IUserInfoTemplateService userInfoTemplateService;
    @Reference
    private ISearchService searchService;
    //@RequireLogin       //表示必须要登陆
    @RequestMapping("")
    public String index(HttpServletRequest request, Model model){
        /*//调用cookie工具类,获取token
        String token = CookieUtil.getToken(request);
        if (token!=null){
            //从redis缓存中取出相应的userinfo
            UserInfo userInfo = userInfoRedisService.getUserInfoByToken(token);
            //共享当前登陆用户信息
            request.getSession().setAttribute("userInfo",userInfo);

        }*/
        model.addAttribute("tcs",travelCommendService.queryCommendTop5());
        model.addAttribute("sc",travelCommendService.queryCommendTop5().get(0));
        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo")TravelQuery qo){
        model.addAttribute("pageInfo",travelService.query(qo));
        return "index/travelPageTpl";
    }

    //选择不同类型的查询,默认为查询全部
    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo")SearchQueryObject qo){
        switch (qo.getType()){
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return destSearch(model,qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return strategySearch(model,qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return travelSearch(model,qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return userSearch(model,qo);
            default:return allSearch(model,qo);
        }
    }

    private String allSearch(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,UserInfoTemplate.TYPE_NAME,UserInfoTemplate.class,qo,"nickname","destName");
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,TravelTemplate.TYPE_NAME,TravelTemplate.class,qo,"title","summary");
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME,StrategyTemplate.TYPE_NAME,StrategyTemplate.class,qo,"title","subTitle","summary");
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME,DestinationTemplate.TYPE_NAME,DestinationTemplate.class,qo,"name","info");

        SearchResultVO vo = new SearchResultVO();
        vo.setDests(dests.getContent());
        vo.setUsers(users.getContent());
        vo.setTravels(travels.getContent());
        vo.setStrategys(strategys.getContent());
        vo.setTotal(dests.getTotalElements()+users.getTotalElements()+travels.getTotalElements()+strategys.getTotalElements());
        model.addAttribute("data",vo);
        return "index/searchAll";
    }

    //用户
    private String userSearch(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,UserInfoTemplate.TYPE_NAME,UserInfoTemplate.class,qo,"nickname","destName");
        model.addAttribute("page",page);
        return "index/searchUser";
    }

    //游记
    private String travelSearch(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,TravelTemplate.TYPE_NAME,TravelTemplate.class,qo,"title","summary");
        model.addAttribute("page",page);
        return "index/searchTravel";
    }

    //攻略
    private String strategySearch(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME,StrategyTemplate.TYPE_NAME,StrategyTemplate.class,qo,"title","subTitle","summary");
        model.addAttribute("page",page);
        return "index/searchStrategy";
    }

    private String destSearch(Model model, SearchQueryObject qo) {

        DestinationTemplate dest = destinationTemplateSearchService.findByName(qo.getKeyword());
        //统计总条数
        long total = 0;
        //创建vo对象
        SearchResultVO data = new SearchResultVO();
        if (dest != null){
            //查该目的地下的所有攻略
            List<StrategyTemplate> sts = strategyTemplateService.findByDestName(dest.getName());
            if (sts.size()>5){
                //获取长度
                total += sts.size();
                //截取前5个
                sts.subList(0,5);
            }
            //查该目的地下的所有游记
            List<TravelTemplate> ts = travelTemplateSearchService.findByDestName(dest.getName());
            if (ts.size()>5){
                total+=ts.size();
                ts.subList(0,5);
            }
            //查该目的地下的所有用户
            List<UserInfoTemplate> us = userInfoTemplateService.findByDestName(dest.getName());
            if (us.size()>5){
                //获取长度
                total += sts.size();
                //截取前5个
                us.subList(0,5);
            }
            data.setStrategys(sts);
            data.setTravels(ts);
            data.setUsers(us);
            data.setTotal(total);
            model.addAttribute("data",data);
        }
        model.addAttribute("dest",dest);
        return "index/searchDest";
    }

}
